import { Message } from "element-ui";
import { map } from "lodash";
import { Toast } from "vant";
//判断接口返回是否正确
export const isCode = (res, need) => {
  if (res.code === 200) {
    if (need) {
      // showMessage(res.message_cn, 'success')
    }
    return true;
  } else {
    if (need !== false) {
      showMessage(res.message_cn, 'error')
    }
    return false;
  }
};

//提示
export const showMessage = (message, type) => {
  if (isPhone(650)) {
    Toast(message);
  } else {
    Message(({
      message: message,
      type: type,
    }));
  }
}

//判断是否是微信内置浏览器
export const isWeixin = () => {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
};

// 判断是否是ios
export const isIOS = () => {
  const u = navigator.userAgent;
  const isios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  if (isios) {
    return true;
  } else {
    return false;
  }
};


//判断是否是手机
export const isPhone = (val) => {
  let screenWidth =
    document.body.clientWidth || document.documentElement.clientWidth;
  if (screenWidth <= val) {
    return true
  } else {
    return false;
  }
};

//弹窗打开停止页面滚动
export const stopBodyMove = () => {
  let move = (e) => {
    e.preventDefault();
  };
  document.body.style.overflow = "hidden";
  document.addEventListener("touchmove", move, { passive: false });
}

//弹窗关闭开启页面滚动
export const startBodyMove = () => {
  let move = (e) => {
    e.preventDefault();
  };
  document.body.style.overflow = "";
  document.removeEventListener("touchmove", move, { passive: true });
}

//监听本地数据
export const watchlocalstorage = () => {
  const signSetItem = localStorage.setItem;
  localStorage.setItem = function (key, val) {
    const setEvent = new Event('setItemEvent');
    setEvent.key = key;
    setEvent.value = val;
    window.dispatchEvent(setEvent);
    signSetItem.apply(this, arguments);
  };
}

//获取时分秒
export const getYMDHMS = (timestamp) => {
  let time = new Date(timestamp * 1000)
  let year = time.getFullYear()
  let month = time.getMonth() + 1
  let date = time.getDate()
  let hours = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()
  if (month < 10) { month = '0' + month }
  if (date < 10) { date = '0' + date }
  if (hours < 10) { hours = '0' + hours }
  if (minute < 10) { minute = '0' + minute }
  if (second < 10) { second = '0' + second }
  return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}

//判断是否是数字
export const isPositiveInteger = (s) => {
  var re = /^[0-9]+$/;
  return re.test(s)
}

//月份转换
export const matchMonth = (time) => {
  if (time) {
    let year = time.split('-')[0];
    let month = time.split('-')[1];
    let months = new Map([['01', 'Jan'], ['02', 'Feb'], ['03', 'Mar'], ['04', 'Apr'], ['05', 'May'], ['06', 'Jun'], ['07', 'Jul'], ['08', 'Aug'], ['09', 'Sep'], ['10', 'Oct'], ['11', 'Nov'], ['12', 'Dec']]);
    let match = months.get(month);
    return match + ',' + year
  } else {
    return 'Unknown'
  }
}



